package cn.org.mrliu.ssm.web.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.mrliu.ssm.common.web.controller.BaseController;
import cn.org.mrliu.ssm.common.web.vo.QueryParam;
import cn.org.mrliu.ssm.common.web.vo.ResponseMessage;
import cn.org.mrliu.ssm.constant.LogConstant;
import cn.org.mrliu.ssm.constant.RequestUrl;
import cn.org.mrliu.ssm.model.Department;
import cn.org.mrliu.ssm.service.DepartmentService;
import cn.org.mrliu.ssm.util.Pagination;

@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController{
	
	private static final Logger log = LoggerFactory.getLogger(DepartmentController.class);
	private static final String DESCRIPTION = "Department";
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 跳转到当前模块的主页面
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.INDEX)
	public String index(Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"index");
		log.debug(LogConstant.MODEL+model);
		return "department/index";
	}
	
	/**
	 * 根据ID获取对象数据
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.DETAIL)
	public String detail(@PathVariable("id")Integer id,Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"detail");
		log.debug(LogConstant.ID,id);
		Department department = departmentService.getModel(id);
		model.addAttribute("department", department);
		log.debug(LogConstant.MODEL+model);
		return "department/detail";
	}
	
	/**
	 * 跳转到添加页面
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.ADD)
	public String add(Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"add");
		log.debug(LogConstant.MODEL+model);
		return "department/add";
	}
	
	/**
	 * 保存数据
	 * @param department
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value=RequestUrl.SAVE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(Department department) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"save");
		log.debug("department:"+department);
		try {
			departmentService.saveModel(department);
			log.info(LogConstant.SAVE_SUCCESS,DESCRIPTION); 
			return new ResponseMessage("保存成功!");
		} catch (Exception e) {
			log.error(LogConstant.SAVE_FAILURE,DESCRIPTION); 
			e.printStackTrace();
		}
		return new ResponseMessage(false, "保存失败!");
	}
	
	/**
	 * 跳转到编辑页面之前获取对象数据
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.EDIT)
	public String edit(@PathVariable("id")Integer id, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"edit");
		log.debug(LogConstant.ID,id);
		Department department = departmentService.getModel(id);
		model.addAttribute("department", department);
		log.debug(LogConstant.MODEL+model);
		return "department/edit";
	}

	/**
	 * 保存数据
	 * @param department
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value=RequestUrl.UPDATE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage update(Department department) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"update");
		log.debug("department:"+department);
		try {
			departmentService.updateModel(department);
			log.info(LogConstant.UPDATE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("修改成功!");
		} catch (Exception e) {
			log.error(LogConstant.UPDATE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "修改失败!");
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value=RequestUrl.DELETE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delete(@PathVariable("id")Integer id) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"delete");
		log.debug(LogConstant.ID,id);
		try {
			departmentService.deleteModel(id);
			log.info(LogConstant.DELETE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("删除成功!");
		} catch (Exception e) {
			log.info(LogConstant.DELETE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false, "删除失败!");
	}
	
	/**
	 * 获取分页列表
	 * @param queryParam 查询参数
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(RequestUrl.LIST)
	public String list(QueryParam queryParam, Model model) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"list");
		log.debug("queryParam:"+queryParam);
		Pagination pagination = departmentService.getPagination(queryParam);
		model.addAttribute("departmentList", pagination);
		log.debug(LogConstant.MODEL+model);
		return "department/list";
	}
	
	/**
	 * 批量保存对象
	 * @param departments JSON数组
	 * @return
	 */
	@RequestMapping(value=RequestUrl.BATCH_SAVE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchSave(@RequestBody Department[] departments) {
		log.debug(LogConstant.EXCUTE_METHOD,"batchSave");
		log.debug("departments:"+departments);
		try {
			departmentService.batchSaveModel(departments);
			log.info(LogConstant.BATCH_SAVE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("批量保存成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_SAVE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false,"批量保存失败!");
	}
	
	/**
	 * 批量修改
	 * @param department
	 * @param ids
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value=RequestUrl.BATCH_UPDATE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchUpdate(Department department,String ids) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"batchUpdate");
		log.debug(LogConstant.ID_String+ids);
		log.debug("department:"+department);
		try {
			String[] idStrings = handleIdString(ids);
			System.out.println(ids);
			log.debug(LogConstant.IDS+ids);
			departmentService.batchUpdateModel(department, idStrings);
			log.info(LogConstant.BATCH_UPDATE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("批量修改成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_UPDATE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false,"批量修改失败!");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value=RequestUrl.BATCH_DELETE,method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage batchDelete(String ids) throws Exception {
		log.debug(LogConstant.EXCUTE_METHOD,"batchDelete");
		log.debug(LogConstant.ID_String+ids);
		try {
			String[] idStrings = handleIdString(ids);
			log.debug(LogConstant.IDS+ids);
			departmentService.batchDeleteModel(idStrings);
			log.info(LogConstant.BATCH_DELETE_SUCCESS,DESCRIPTION);
			return new ResponseMessage("批量删除成功!");
		} catch (Exception e) {
			log.error(LogConstant.BATCH_DELETE_FAILURE,DESCRIPTION);
			e.printStackTrace();
		}
		return new ResponseMessage(false,"批量删除失败!");
	}
	
}
